package com.example.zhilongzhang.constructionpayapp.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.example.zhilongzhang.constructionpayapp.R;
import com.example.zhilongzhang.constructionpayapp.base.BaseActivity;
import com.example.zhilongzhang.constructionpayapp.base.ToastManager;
import com.example.zhilongzhang.constructionpayapp.bean.QrCodeBean;
import com.example.zhilongzhang.constructionpayapp.bean.UserInfoBean;
import com.example.zhilongzhang.constructionpayapp.dialog.WaitDialog;
import com.example.zhilongzhang.constructionpayapp.http.HttpMethod;
import com.example.zhilongzhang.constructionpayapp.method.bluetooth.ToothMethod;
import com.example.zhilongzhang.constructionpayapp.util.SpUtil;
import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

public class SetMonetActivity extends BaseActivity {
    EditText edAddress, edMoney, edNote;
    int i = 0;

    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_monet);
        edAddress = findViewById(R.id.ed_address);
        edMoney = findViewById(R.id.ed_money);
        edNote = findViewById(R.id.ed_note);
        initData();

    }

    private WaitDialog mDialog;
    boolean isTooth = false;

    private void initData() {
        findViewById(R.id.left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.tv_recognition_receipts).setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(edMoney.getText())) {
                    ToastManager.getInstance(SetMonetActivity.this).showToast("请输入金额");
                    return;
                }
                if (Double.parseDouble(edMoney.getText().toString()) <= 0) {
                    ToastManager.getInstance(SetMonetActivity.this).showToast("金额要大于0");
                    return;
                }
                mDialog = new WaitDialog(SetMonetActivity.this);
                mDialog.show();

                Log.e("daying", "SpUtil=" + SpUtil.getDaYin());
                if (SpUtil.getDaYin()) {
                    if (!ToothMethod.checkBluetoothState(SetMonetActivity.this)) {
                        if (isTooth) {
                            if (TextUtils.isEmpty(SpUtil.getShopSearUrl())) {
                                getData(edMoney.getText().toString());
                            } else {
                                getQrCode(edMoney.getText().toString());
                            }
                        } else {
                            ToastManager.getInstance(SetMonetActivity.this).showToast("请开启蓝牙,启动打印功能");
                            startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS));
                            isTooth = true;
                            mDialog.dismiss();
                        }
                    } else {
                        if (TextUtils.isEmpty(SpUtil.getShopSearUrl())) {
                            getData(edMoney.getText().toString());
                        } else {
                            getQrCode(edMoney.getText().toString());
                        }
                    }
                } else {
                    if (TextUtils.isEmpty(SpUtil.getShopSearUrl())) {
                        getData(edMoney.getText().toString());
                    } else {
                        getQrCode(edMoney.getText().toString());
                    }
                }
            }
        });
    }

    //二维码的生成
    String strNote = "";

    private void getQrCode(final String money) {
        if (TextUtils.isEmpty(SpUtil.getShopSearUrl())) {
            ToastManager.getInstance(SetMonetActivity.this).showToast("您没有绑定商户，请先绑定,然后重新登录");
            return;
        }
        if (edNote.getText() != null) {
            strNote = edNote.getText().toString();
        }
        HttpMethod.getQrCode(SetMonetActivity.this, money, new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                mDialog.dismiss();
            }

            @Override
            public void onResponse(String response) {
                i = i + 1;
                QrCodeBean bean = new Gson().fromJson(response, QrCodeBean.class);
                if (bean.getQrBase64() == null && i >= 3) {
                    ToastManager.getInstance(SetMonetActivity.this).showToast("该商户未绑定银行，无法获取二维码");
                    return;
                }
                if (bean.getOrderNo() == null) {
                    ToastManager.getInstance(SetMonetActivity.this).showToast("该商户未绑定银行，无法获取二维码");
                    return;
                }
                Intent intent = new Intent(SetMonetActivity.this, MoneyCodeActivity.class);
                intent.putExtra("qrBase64", bean.getQrBase64());
                intent.putExtra("orderId", bean.getOrderNo());
                intent.putExtra("money", money);
                intent.putExtra("note", strNote);
                intent.putExtra("born", "set");
                startActivity(intent);
                mDialog.dismiss();
                finish();

            }
        });

    }

    private void getData(final String money) {
        HttpMethod.security(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                if (response == null) {
                    return;
                }
                UserInfoBean bean = new Gson().fromJson(response, UserInfoBean.class);
                if (bean.getDept() != null) {
                    SpUtil.setName(bean.getRealName());
                    SpUtil.setShopName(bean.getDept().getDeptName());
                    SpUtil.setShopSearUrl(String.valueOf(bean.getDept().getDepReqUrl()));
                    SpUtil.setShopAddUrl(String.valueOf(bean.getDept().getDepSaveUrl()));
                    SpUtil.setQRImgUrl(String.valueOf(bean.getDept().getQrReqUrl()));
                    SpUtil.setQRPayUrl(String.valueOf(bean.getDept().getQrSaveUrl()));
                    SpUtil.setShopId(String.valueOf(bean.getDept().getId()));
                }
                if (bean.getTel() != null) {
                    SpUtil.setTel(bean.getTel());
                }
                SpUtil.setUserId(String.valueOf(bean.getId()));
                getQrCode(money);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
    }
}
